'use strict';
angular.module('app')
    .controller('messagePlatController', [ '$rootScope', '$scope', '$http', '$state','$timeout',
        function($rootScope, $scope, $http, $state, $timeout) {
    	
            $scope.title = '资讯信息管理';
            $scope.searchParams = { };
            $scope.loading = false;
            $scope.param = {};
            
            $scope.search = function () {
            	$scope.selected = [];
                $scope.loading = true;
                
                $("#allCheck").attr("checked", false);
                $.ajax({
                    url : $scope.app.project_name + '/messagePlat/read/list',
                    data: $scope.searchParams
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.searchRe = function () {
            	$scope.selected = [];
                $scope.loading = true;
                
                $("#allCheck").attr("checked", false);
                $scope.searchParams.pageNum = 1;
                $.ajax({
                    url : $scope.app.project_name + '/messagePlat/read/list',
                    data: $scope.searchParams
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
            
            $scope.getMessagePlatList = function (){
                $.ajax({
                    url : $scope.app.project_name + '/messagePlat/read/getMessagePlatList'
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.messagePlatList = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
    		}

            $scope.search();
            $scope.getMessagePlatList();

            $scope.clearSearch = function() {
                $scope.searchParams = { };
                $scope.search();
            }
            
            $scope.selected = [];
    		var updateSelected = function (action, contact) {
    			if (action == 'add' && $scope.selected.indexOf(contact.messageId) == -1){
    				$scope.selected.push(contact.messageId);
    				contact.checked = true;
    			}

    			if (action == 'remove' && $scope.selected.indexOf(contact.messageId) != -1){
    				$scope.selected.splice($scope.selected.indexOf(contact.messageId), 1);
    				contact.checked = false;
    			}
    		};

    		$scope.updateSelection = function ($event, contact) {
    			var checkbox = $event.target;
    			var action = (checkbox.checked ? 'add' : 'remove');
    			if (action == 'add' && $scope.selected.indexOf(contact.messageId) == -1){
    				$scope.selected.push(contact.messageId);
    				contact.checked = true;
    			}
    			if (action == 'remove' && $scope.selected.indexOf(contact.messageId) != -1){
    				$scope.selected.splice($scope.selected.indexOf(contact.messageId), 1);
    				contact.checked = false;
    			}
    		};

    		// 全选操作
            $scope.selectAll = function ($event) {
            	var checkbox = $event.target;
            	var action = (checkbox.checked ? 'add' : 'remove');
            	
            	for (var i = 0; i < $scope.pageInfo.records.length; i++) {
            		var contact = $scope.pageInfo.records[i];
            		updateSelected(action, contact);
            	}
            };
            
            $scope.isSelected = function (id) {
            	return $scope.selected.indexOf(id) >= 0;
            };

            // 翻页
            $scope.pagination = function (pageNo) {
                $scope.searchParams.pageNum = pageNo;
                $scope.search();
            };

            $scope.enable = function(id, enable) {
            	var ids = [];
            	
                if (id == null || id == '') {
                    ids = $scope.selected;
                } else {
                    ids.push(id);
                }

                var idStr = ids.join(',');
                if (idStr == null || idStr == '') {
                    $scope.alertModal({"content":"请选择需要启用或禁用的记录"});
                    return;
                }
                
                var msg = '';
                // 判断是否有不符合操作的记录
                var count = 0;
                var nocount = 0;
            	if (enable == 'Y') {
            		msg = '启用';
            		
            		angular.forEach($scope.pageInfo.records, function (item) {
                        if (item.enable == 'N' && item.checked) {
                            count++;
                        } else if (item.checked && item.enable != 'N') {
                            nocount++;
                        }
                    })
            	}
            	
            	if (enable == 'N') {
            		msg = '停用';
            	}
                
                if (nocount > 0) {
                    $scope.alertModal({"content":msg + count + "条,不符合操作条件的" + nocount + "条"});
                    return;
                }
            	
            	$scope.alertMessage({
                    text:"确定要" + msg + "选中的资讯吗？", //内容
                    btnCancel:true, //取消按钮
                    btnConfirm:true //确认按钮
                }).then(function () {
        			$.ajax({
        				url : $scope.app.project_name + '/messagePlat/update/enable',
        				data: {'ids': idStr, 'enable':enable},
        				traditional: true
        			}).then(function(result) {
        		        $scope.loading = false;
        				if (result.httpCode == 200) {
        					$scope.param.keyword = null;
        					$scope.selected = [];
        					$scope.search();
        				} else {
        					$scope.msg = result.msg;
        				}
        			});
                })
            }
            
            $scope.isRelease = function(id, isrelease) {
            	var ids = [];
            	
                if (id == null || id == '') {
                    ids = $scope.selected;
                } else {
                    ids.push(id);
                }

                var idStr = ids.join(',');
                if (idStr == null || idStr == '') {
                    $scope.alertModal({"content":"请选择需要上架或下架的记录"});
                    return;
                }
                
                var msg = '';
                // 判断是否有不符合操作的记录
                var count = 0;
                var nocount = 0;
            	if (isrelease == 'Y') {
            		msg = '上架';
            		
            		angular.forEach($scope.pageInfo.records, function (item) {
                        if (item.isrelease == 'N' && item.checked && item.enable == 'Y') {
                            count++;
                        } else if (item.checked && (item.isrelease != 'N' || item.enable != 'Y')) {
                            nocount++;
                        }
                    })
            	}
            	
            	if (isrelease == 'N') {
            		msg = '下架';
            		
            		angular.forEach($scope.pageInfo.records, function (item) {
                        if (item.isrelease == 'Y' && item.checked && item.enable == 'Y') {
                            count++;
                        } else if (item.checked && item.isrelease != 'Y' && item.enable != 'Y') {
                            nocount++;
                        }
                    })
            	}
                
                if (nocount > 0) {
                    $scope.alertModal({"content":msg + count + "条,不符合操作条件的" + nocount + "条"});
                    return;
                }
            	
            	$scope.alertMessage({
                    text:"确定要" + msg + "选中的资讯吗？", //内容
                    btnCancel:true, //取消按钮
                    btnConfirm:true //确认按钮
                }).then(function () {
        			$.ajax({
        				url : $scope.app.project_name + '/messagePlat/update/isRelease',
        				data: {'ids': idStr, 'isrelease':isrelease},
        				traditional: true
        			}).then(function(result) {
        		        $scope.loading = false;
        				if (result.httpCode == 200) {
        					$scope.param.keyword = null;
        					$scope.selected = [];
        					$scope.search();
        				} else {
        					$scope.msg = result.msg;
        				}
        			});
                })
            }

            $scope.messageImg = [];
            $scope.preview2 = function(messageImg) {
                if (messageImg != null && messageImg != '') {
                    $scope.messageImg = messageImg.split('|');;
                }
                $scope.firstApply = true;
            }

            // 鼠标移入的动作
            $scope.introduction = [];
            $scope.preview = function(introduction) {
                var showDiv = document.getElementById('showDiv');
                showDiv.style.left = event.clientX;
                showDiv.style.top = event.clientY;
                showDiv.style.display = 'block';
                showDiv.innerHTML = introduction;
            }
            
            // 鼠标移出的动作
            $scope.outHide = function() {
                var showDiv = document.getElementById('showDiv');
                showDiv.style.display = 'none';
                showDiv.innerHTML = '';
            }

            // 鼠标移入的动作
            $scope.introduction = [];
            $scope.preview1 = function(introduction) {
                var showDiv1 = document.getElementById('showDiv');
                showDiv1.style.left = event.clientX;
                showDiv1.style.top = event.clientY;
                showDiv1.style.display = 'block';
                showDiv1.innerHTML = introduction;
            }
            
            // 鼠标移出的动作
            $scope.outHide1 = function() {
                var showDiv1 = document.getElementById('showDiv');
                showDiv1.style.display = 'none';
                showDiv1.innerHTML = '';
            }
            
            $scope.stopShow = function () {
                $scope.myShow = false;
            };
            
            //预览
            $scope.onPreview = function (data) {
                console.log(data)
                $scope.myShow = true;
                $scope.dataUrl = data.messageVideo;
            };
            
            $scope.getFileExt=function (str) {
                var d=/\.[^\.]+$/.exec(str);
                return d[0].toLowerCase();
            }

        } ]);